from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from util.fileUtil import USER_HEAD_IMG_FILE_PATH, DEFAULT_USER_HEAD_IMG_FILE_PATH


def user_head_img_api(request, id):
    """
    用户头像接口
    """
    user_head_img_path = USER_HEAD_IMG_FILE_PATH / f"{id}.png"
    if user_head_img_path.exists():
        img_path = user_head_img_path
    else:
        img_path = DEFAULT_USER_HEAD_IMG_FILE_PATH
    with open(img_path, 'rb') as img_file:
        img_data = img_file.read()
    return HttpResponse(img_data, content_type="image/jpeg")


